import { resourceCustomAdd, resourceCustomUpdate } from "@/services/chargingManage";
import { DatePicker, Form, Input, InputNumber, message, Modal } from "antd";
import moment from "moment";
import React from "react";

class PriceResource extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      visible:false,
      data:null
    }
  }

  componentDidMount(){
    if(this.props.onRef){
      this.props.onRef(this)
    }
  }

  initModal = data => {
    this.setState({
      visible:true,
      data
    },() => {
      if(data){
        this.props.form.setFieldsValue({
          resourceName:data.resourceName,
          resourceUnit:data.resourceUnit,
          standardPrice:data.standardPrice,
        })
        if(data.resourceType != 16){
          this.props.form.setFieldsValue({overflowPrice:data.overflowPrice})
        }
      }
    })
  }

  doCancel = () => {
    this.props.form.resetFields()
    this.setState({
      visible:false,
      data:null
    })
  }

  validateForm = () => {
    this.props.form.validateFields((error,values) => {
      if(!error){
        console.log(values);
        this.doOk(values)
        
      }
    })
  }

  doOk = async values => {
    const { data } = this.state;
    values.id = data.id
    const res = await resourceCustomUpdate(values)

    if(res.code == -1){}else{
      message.info(res.message)
      this.props.refreshList()
      this.doCancel()
    }
  }

  render(){
    const { visible,data } = this.state;
    const { form:{getFieldDecorator} } = this.props;

    return (
      <Modal
        title='资源定价'
        visible={visible}
        onCancel={this.doCancel}
        onOk={this.validateForm}
        centered={true}
      >
        <Form labelCol={{ span: 4 }} wrapperCol={{ span: 20 }}>
          <Form.Item label='资源名称'>
            {
              getFieldDecorator('resourceName')(
                <Input disabled />
              )
            }
          </Form.Item>
          <Form.Item label='单位'>
            {
              getFieldDecorator('resourceUnit')(
                <Input disabled />
              )
            }
          </Form.Item>
          <Form.Item label='标准定价'>
            {
              getFieldDecorator('standardPrice')(
                <InputNumber min={0} />
              )
            }
            <span style={{marginLeft:10}}>元</span>
          </Form.Item>
          {
            data && data.resourceType == 16 ? '' : (
              <Form.Item label='溢出定价'>
                {
                  getFieldDecorator('overflowPrice')(
                    <InputNumber min={0} />
                  )
                }
                <span style={{marginLeft:10}}>元</span>
              </Form.Item>
            )
          }
          
        </Form>
      </Modal>
    )
  }
}

export default Form.create()(PriceResource)